﻿using Microsoft.AspNetCore.Components;

namespace EasyXaf.Blazor.ColorPickers;

public partial class ColorPickerRenderer
{
    private string Id { get; } = $"id-{Guid.NewGuid()}";

    private bool IsOpen { get; set; }

    private string Color
    {
        get => Model.Value?.ToString();
        set => Model.Value = value;
    }

    private string BackgroundStyle
    {
        get
        {
            if (!string.IsNullOrWhiteSpace(Color) && Color.StartsWith("#"))
            {
                return $"background-color:{Color};";
            }
            else
            {
                return "background-image:repeating-linear-gradient(45deg, #EFEFEF, #EFEFEF 3px, #FFF 3px, #FFF 6px);";
            }
        }
    }

    [Parameter]
    public ColorPickerModel Model { get; set; }

    private void OnTextChanged(string text)
    {
        Color = text;

        if (Model.HasValueChanged)
        {
            Model.ValueChanged.Invoke(text);
        }
    }

    private void OnColorChanged(string color)
    {
        Color = color;
        IsOpen = false;

        if (Model.HasValueChanged)
        {
            Model.ValueChanged.Invoke(color);
        }
    }
}